package com.sdry.web.controller.approval;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.entity.agreement.Agreement;
import com.sdry.entity.approval.Approval;
import com.sdry.entity.approval.ApprovalExample;
import com.sdry.entity.approval.ApprovalExample.Criteria;
import com.sdry.entity.base.Staff;
import com.sdry.service.approval.ApprovalService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName WaitAppController
 * @Description 待审批
 * @Author lz
 * @Date 2018年8月30日 11:07:46
 * @Version 1.0
 */
@Controller
@RequestMapping("/approval")
public class WaitAppController {
	
	@Resource
	ApprovalService approvalService;

	/**
	 * 进入待审批页面
	 * @return
	 */
	@RequestMapping("/waitApproval")
	public String waitApproval(){
		return "/approval/waitApproval";
	}
	
	
	/**
	 * 分页查询待我审批
	 * @param request
	 * @param response
	 * @param limit
	 * @param offset
	 * @param approval
	 * @param session
	 * @throws Exception
	 */
    @RequestMapping("findWaitApproved")
    public void findWaitApproved(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "limit",required = false)int limit,
                                   @RequestParam(value = "offset",required = false)int offset,
                                   Approval approval,HttpSession session) throws Exception {
    	
    	 Staff userSession = (Staff) session.getAttribute("staff");
    	 //获取当前登录人
    	 String sname = userSession.getSname();
    	 //获取当前登录人的部门
         String dname = userSession.getDepartment().getDname();
    	
    	PageHelper.startPage(offset, limit);
    	
    	List<Approval> list2 = new ArrayList<>();
    	
    	ApprovalExample example = new ApprovalExample();
    	ApprovalExample example2 = new ApprovalExample();
    	Criteria createCriteria = example.createCriteria();
    	Criteria createCriteria2 = example2.createCriteria();
    	
    	//查询条件：获取提交审批的人
    	if(approval.getApplicant() != null && approval.getApplicant() != "") {
    		createCriteria.andApplicantLike("%"+approval.getApplicant()+"%");
    	}
    	//查询条件：审批文件类型
    	if(approval.getStatus03()!= null && approval.getStatus03() != "") {
    		createCriteria.andStatus03Like("%"+approval.getStatus03()+"%");
    	}
    	
    	//查询条件：获取提交审批的人
    	if(approval.getApplicant() != null && approval.getApplicant() != "") {
    		createCriteria2.andApplicantLike("%"+approval.getApplicant()+"%");
    	}
    	//查询条件：审批文件类型
    	if(approval.getStatus03()!= null && approval.getStatus03() != "") {
    		createCriteria2.andStatus03Like("%"+approval.getStatus03()+"%");
    	}
    	
    	//查询含该部门的记录
    	createCriteria.andStatus02Like("%"+dname+"%");
    	createCriteria2.andStatus02Like("%"+sname+"%");
    	
		List<Approval> list = approvalService.selectByExample(example);
		String department = "财务部";
		if(!department.equals(dname)) {
			List<Approval> peopleList = approvalService.selectByExample(example2);
			list.addAll(peopleList);
		}
		Collections.sort(list); 
		for (Approval approval2 : list) {
			//日期处理
			Date approvaltime = approval2.getApprovaltime();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String format = dateFormat.format(approvaltime);
			approval2.setRemark05(format);
			
			String[] split = approval2.getStatus02().split(",");
			
			int index = 0;
			String x = "1";
			if(approval2.getTypes() != null  && approval2.getTypes().equals(x)) {
				
				if(department.equals(dname)) {
					sname = sname+"[财务部]";
				}
				
				for (int i = 0; i < split.length; i ++) {
					if(sname.equals(split[i])) {
						index = i;
					}
				}
			}else {
				for (int i = 0; i < split.length; i ++) {
					if(dname.equals(split[i])) {
						index = i;
					}
				}
			}
			
			if(approval2.getStatus01().equals("a2") && approval2.getStatus01().equals("b2") && approval2.getStatus01().equals("c2") 
					&& approval2.getStatus01().equals("d2")) {
				approval2.setRemark04("审批完成");
			}else if(split.length == 1 && approval2.getStatus02().equals("a1")) {
				approval2.setRemark04("审批完成");
			}else if(split.length == 2 && approval2.getStatus02().equals("b1")) {
				approval2.setRemark04("审批完成");
			}else if(split.length == 3 && approval2.getStatus02().equals("c1")) {
				approval2.setRemark04("审批完成");
			}else if(split.length == 4 && approval2.getStatus02().equals("d1")) {
				approval2.setRemark04("审批完成");
			}else {
				approval2.setRemark04("审批中");
				
				//一级审批
				if(index == 0) {
					
					if(approval2.getStatus01().equals("0")) {
						approval2.setRemark04(dname);
						list2.add(approval2);
					}
				}
				
				//二级审批
				if(index == 1) {
					if(approval2.getStatus01().equals("a1")) {
						approval2.setRemark04(dname);
						list2.add(approval2);
					}
				}
				
				//三级审批
				if(index == 2) {
					if(approval2.getStatus01().equals("b1")) {
						approval2.setRemark04(dname);
						list2.add(approval2);
					}
				}
				
				//四级审批
				if(index == 3) {
					if(approval2.getStatus01().equals("c1")) {
						approval2.setRemark04(dname);
						list2.add(approval2);
					}
				}
			}
		}
		PageInfo<Approval> info = new PageInfo<Approval>(list2);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", info.getTotal());
        jsonObject.put("rows", info.getList());
        ResponseUtil.write(response, jsonObject);
    }
    
    /**
     * 根据审批id查询项目文档为表单的数据集合
     * @param response
     * @param aremark
     * @throws Exception 
     */
    @RequestMapping("/selectAgreementByApprovalId")
    public void selectAgreementByApprovalId(HttpServletResponse response, String aremark) throws Exception {
    	 List<Agreement> list= approvalService.selectAgreementByApprovalId(aremark);
    	 if (list.size() > 0) {
 			JSONArray jsonArray = JSONArray.fromObject(list);
 			ResponseUtil.write(response, jsonArray);
 		} 
    }
}
